// 国际化语言模块
import storage from '@/utils/storage.js'
import { LANGUAGE } from '@/store/mutationTypes.js'

// 获取cookie中的LANGUAGE
const languageCookie = storage.getCookie(LANGUAGE)

const state = {
  language: languageCookie !== '0' ? languageCookie : 'zh'
}

const mutations = {
  setLang: (state, lang) => {
    state.language = lang
  }
}

const actions = {
  setLanguage: ({ commit }, lang) => {
    storage.setCookie(LANGUAGE, lang)
    commit('setLang', lang)
    return Promise.resolve(lang)
  }
}

const getters = {
  language: (state) => state.language
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
